package com.zg.ktcomwan.adapter

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.zg.ktcomwan.R
import com.zg.ktcomwan.base.BaseAdapter
import com.zg.ktcomwan.databinding.ArticleItemBinding
import com.zg.ktcomwan.model.ArticleBean

/***
 * @Author:      zxd
 * @CreateDate:  2021/8/20 17:21
 * @Description: 测试 基础列表碎片
 */
/*
class TestAdapter : PagingDataAdapter<ArticleBean, RecyclerView.ViewHolder>(ArticleDiffCallback()) {
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        TODO("Not yet implemented")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        TODO("Not yet implemented")
    }
}*/

class TestAdapter(private val context: Context) :
    BaseAdapter<ArticleBean, TestAdapter.ViewHolder>(ArticleDiffCallback()) {
    class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: ArticleBean, position: Int, context: Context) {
            val binding: ArticleItemBinding = binding as ArticleItemBinding
            binding.setArticle(item)
            binding.setContext(context)
            binding.root.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("url", item.link)
                bundle.putString("title", item.title)
                Navigation.findNavController(it).navigate(R.id.navigation_web, bundle)
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let { holder.bindData(it, holder.layoutPosition, context) };
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            viewType,
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.article_item
    }
}